package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.News;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IQuestionService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("questions")
public class QuestionController {

    @Autowired
    private IQuestionService questionService;

    /**
     * 列出所有问题
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo query(QueryObject qo){
        qo.setOrderBy("createTime desc");
        return questionService.listAll(qo);
    }

    /**
     * 保存问题
     * @param question
     * @return
     */
    @PostMapping
    public JsonResult save(Question question){
        if (question.getId()==null){
            question.setCreateTime(new Date());
            question.setUser(UserContext.getCurrentUser());
            questionService.save(question);
        }
        return new JsonResult();
    }

    /**
     * 根据id查询单个问题
     * @param questionId
     * @return
     */
    @GetMapping("/{questionId}")
    public Question getById(@PathVariable Long questionId){
        Question question = questionService.getById(questionId);
        return question;
    }
}
